package com.jswl.training.springjpa.controller;

import com.jswl.training.springjpa.annotation.Action;
import com.jswl.training.springjpa.common.state.Result;
import com.jswl.training.springjpa.config.ConfigYml;
import com.jswl.training.springjpa.entity.User;
import com.jswl.training.springjpa.netty.client.NettyClient;
import com.jswl.training.springjpa.util.HexByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class RequestController {


    /*读取配置文件**************************************/
    private ConfigYml configYml;

    @Value("${myname}")
    private  String myName;

    public RequestController(ConfigYml configYml){
        this.configYml=configYml;
    }

    @RequestMapping("/configYml")
    public Result configYml(){
        return Result.ok(configYml);
    }

    @RequestMapping("/configValue")
    public Result configValue() {
        return Result.ok(myName);
    }

    /*参数接收方式************************************************/
    /**
     * @RequestParam 接收参数
     * @param name
     * @param pwd
     * @return
     */
    @Action(name="requestParam 注解")
    @RequestMapping(value = "/requestParam",method = RequestMethod.GET)
    //@GetMapping(value = "/requestParam")
    public Result requestParam(@RequestParam("username") String name,
                               @RequestParam("password") String pwd,
                               @RequestParam Map<String,String> params) {
        Map<String,Object> map = new HashMap<>();
        map.put("username", name);
        map.put("password", pwd);
        map.put("params", params);

        List<Integer> integers = Arrays.asList(1, 2, 3);
        for (Integer integer : integers) {

        }
        for (int i = 0; i < integers.size(); i++) {

        }
        boolean flag = true;
        return  Result.ok(map);
    }

    public static void main(String[] args) {
        System.out.println("fafnnfa");
        System.out.println("info");
        System.out.println("test");

    }

    /**
     * 直接接收对象
     * @param user
     * @return
     */
    @RequestMapping("/requestModel")
    public Result requestModel(User user) {
        return  Result.ok(user);
    }

    /**
     * @PathVariable 接收路径参数
     * @param id
     * @return
     */
    @RequestMapping("/pathVariable/{id}")
    public Result requestModel(@PathVariable("id") String id) {
        return Result.ok(id);
    }

    /**
     * 前端传递json数据
     * @param user
     * @return
     */
    @PostMapping("/requestJson")
    public Result requestJson(@RequestBody User user){
        return Result.ok(user);
    }

    /**
     * @RequestHeader 接收数据
     * @param auth
     * @return
     */
    @PostMapping("/requestHeader")
    public Result requestHeader(@RequestHeader("auth") String auth) {
        return Result.ok(auth);
    }

    @PostMapping("/servlet")
    public Result requestHeader(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        return Result.ok(parameterMap);
    }


    @Autowired
    private NettyClient client;
    @PostMapping("/netty")
    public Result netty(@RequestParam("msg") String msg) {
        String hexMsg = "7B 7B 7B 00 00 00 00 A2 30 01 62 00 60 09 0B 00 94 02 00 00 11 D3 11 2C 01 11 40 42 0F 00 00 00 00 " +
                "00 0B 00 00 00 0B 00 0B 00 0B 00 0C 00 D0 02 00 00 22 D4 11 2C 01 11 40 42 0F 00 00 00 00 00 0C 00 00 00 0C 00 0C 00 0C " +
                "00 0D 00 0C 03 00 00 33 D5 11 2C 01 11 40 42 0F 00 00 00 00 00 0D 00 00 00 0D 00 0D 00 0D 00 0E 00 48 03 00 00 44 D6 11 " +
                "2C 01 11 40 42 0F 00 00 00 00 00 0E 00 00 00 0E 00 0E 00 0E 00 0F 00 84 03 00 00 55 D7 11 2C 01 11 40 42 0F 00 00 00 00 " +
                "00 0F 00 00 00 0F 00 0F 00 0F 00 10 00 C0 03 00 00 11 D8 11 2C 01 11 40 42 0F 00 00 00 00 00 10 00 00 00 10 00 10 00 10 " +
                "00 11 00 FC 03 00 00 11 D9 11 2C 01 11 40 42 0F 00 00 00 00 00 11 00 00 00 11 00 11 00 11 00 12 00 38 04 00 00 33 DA 11 " +
                "2C 01 11 40 42 0F 00 00 00 00 00 12 00 00 00 12 00 12 00 12 00 13 00 74 04 00 00 44 DB 11 2C 01 11 40 42 0F 00 00 00 00 " +
                "00 13 00 00 00 13 00 13 00 13 00 14 00 B0 04 00 00 55 DC 11 2C 01 11 40 42 0F 00 00 00 00 00 14 00 00 00 14 00 14 00 14 " +
                "00 F8 00 7B 7B 7B 00 00 00 00 A3 7E 00 61 00 D0 03 01 11 00 7E CA 85 F5 FF FF FF 00 82 35 7A 0A 00 00 00 00 7E CA " +
                "85 F5 FF FF FF 00 FA E2 0F 96 FF FF FF 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 " +
                "7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 " +
                "7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 00 82 35 7A 0A 00 00 00 01 00" ;
        byte[] msgBytes = HexByteUtil.hexStrToByteArrs(hexMsg);
        int haveSendNum=0;
        ByteBuf byteBuf = Unpooled.buffer();
        for (byte msgByte : msgBytes) {
            byteBuf.writeByte(msgByte);
            haveSendNum++;

            try {
                if(haveSendNum%100==0 || haveSendNum>=msgBytes.length){
                    client.sendMsg(byteBuf);
                    byteBuf = Unpooled.buffer();
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return Result.ok("success!");


    }


}
